<?php
/*
本系统由苏酷伊基于SiteHub项目二次开发
作者博客： https://www.sukuy.com/
后续更新地址： https://www.sukuy.com/navigation
作者QQ：3288637559
QQ交流群：884250547
任何第三方非官网在发布本系统时，不得删除该注释、版权信息、作者信息及官网等内容
*/
define('IN_ADMIN', true);
include("../includes/common.php");
if ($admin_islogin == 1) {
} else exit("<script language='javascript'>window.location.href='./login.php';</script>");

$act = _get('act');
switch ($act) {
    case 'add':
        $content = _post('content');
        if (!$content) {
            
            add_action_log($conf['admin_user'], '添加公告失败：内容不能为空', 0);
            exit("<script>alert('内容不能为空！');location='./notice.php';</script>");
        }
        $result = $DB->insert('notice', array('content' => $content));
        if ($result) {
            
            $log_content = mb_substr($content, 0, 50, 'utf-8') . (mb_strlen($content, 'utf-8') > 50 ? '...' : '');
            add_action_log($conf['admin_user'], '添加公告：' . $log_content, 1);
            exit("<script>alert('添加成功！');location='./notice.php';</script>");
        } else {
            
            add_action_log($conf['admin_user'], '添加公告失败', 0);
            exit("<script>alert('添加失败！');history.go(-1)</script>");
        }
        break;
    case 'edit':
        $id = _post('id');
        $row = $DB->find('notice', '*', array('id' => $id));
        if (!$row) {
            
            add_action_log($conf['admin_user'], '编辑公告失败(ID: ' . $id . ')：公告不存在', 0);
            exit("<script>alert('分类不存在！');location='./notice.php';</script>");
        }
        $content = _post('content');
        if (!$content) {
            
            add_action_log($conf['admin_user'], '编辑公告失败(ID: ' . $id . ')：内容不能为空', 0);
            exit("<script>alert('内容不能为空！');history.go(-1)</script>");
        }
        
        $update_data = array(
            'id' => $id,
            'content' => $content
        );
        $result = $DB->update('notice', $update_data, array('id' => $id));
        if ($result) {
            
            $log_content = mb_substr($content, 0, 50, 'utf-8') . (mb_strlen($content, 'utf-8') > 50 ? '...' : '');
            add_action_log($conf['admin_user'], '编辑公告(ID: ' . $id . ')：' . $log_content, 1);
            exit("<script>alert('修改成功！');location='./notice.php';</script>");
        } else {
            
            add_action_log($conf['admin_user'], '编辑公告失败(ID: ' . $id . ')', 0);
            exit("<script>alert('修改失败！');history.go(-1)</script>");
        }
        break;
    case 'del':
        @header('Content-Type: application/json; charset=UTF-8');
        $id = _post('id');
        if (!$id) {
            exit('{"code":-1,"msg":"ID不能为空！"}');
        }
        
        $notice_info = $DB->find('notice', 'content', array('id' => $id));
        $notice_content = $notice_info ? mb_substr($notice_info['content'], 0, 50, 'utf-8') . (mb_strlen($notice_info['content'], 'utf-8') > 50 ? '...' : '') : '未知公告';
        $result = $DB->delete('notice', array('id' => $id));
        if ($result) {
            
            add_action_log($conf['admin_user'], '删除公告(ID: ' . $id . ')：' . $notice_content, 1);
            exit('{"code":0,"msg":"删除成功！"}');
        }
        
        add_action_log($conf['admin_user'], '删除公告失败(ID: ' . $id . ')', 0);
        exit('{"code":-1,"msg":"删除失败！"}');
        break;
    default:
        @header('Content-Type: application/json; charset=UTF-8');
        exit('{"code":-4,"msg":"No Act"}');
        break;
}
